$(function(){

	let date = (new Date()).getFullYear()
	let chooseDateHtml = '<option></option>'
	for(let year = date-10; year <= date; year++){
		chooseDateHtml += `<option>${year}</option>`
	}
	$('#choseYear_projectManage').html(chooseDateHtml)



	$.ajax({
		url:'/Admin/Project/getPros',
		type:'get',
		success:function(data){
			let datas = data.data
			if(datas.length>0){
				let html = `<li><a href="/Admin/Project/addPro" class="add_pro"></a></li>`
				for(let key of datas){
					// console.log(key)
					html += `
						<li>
					        <div class="pro_list_top">
					            <p class="pro_list_top_proName">${key.pro_name}</p>
					            <p class="pro_list_top_proNum">项目编号：${key.pro_sn}</p>
					        </div>
					        <div class="pro_list_middle">
					        `
					        if(key.pro_method=='自主研发'){
					        	html += `
					        		<div class="pro_list_middle_bigO zz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='委托研发'){
								html += `
					        		<div class="pro_list_middle_bigO wt">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='集中研发'){
								html += `
					        		<div class="pro_list_middle_bigO jz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='合作研发'){
								html += `
					        		<div class="pro_list_middle_bigO hz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }
				            html += `
					            <ul class="pro_list_middle_info">
					                <li>开始时间：${key.pro_start||'-'}</li>
					                <li>结束时间：${key.pro_end||'-'}</li>
					                <li>经费预算：${key.pro_total||'-'}</li>
					            </ul>
					        </div>
					        <ul class="pro_list_bottom">
					            <li><a href="/admin/project/editPro?id=${key.id}">查看</a></li>
					            <li><a href="#" class='exportFile' data-id='${key.id}'>导出项目备查文件</a></li>
					        </ul>
					        <a href="" class="delete_btn" data-id='${key.id}'>删除</a>
					    </li>
					`
				}
				$('.pro_list').html(html)
			}
		}
	})


	$('#tap_list').on('click','a',function(e){
		e.preventDefault()
		let is_approve = $(this).attr('href')
		$(this).addClass('active')
		$(this).parent().siblings().children().removeClass('active')
		$.ajax({
			url:'/Admin/Project/getPros',
			type:'get',
			data:{
				is_approve:is_approve
			},
			success:function(data){
				let datas = data.data
				let html = `<li><a href="/Admin/Project/addPro" class="add_pro"></a></li>`
				for(let key of datas){
					html += `
						<li>
					        <div class="pro_list_top">
					            <p class="pro_list_top_proName">${key.pro_name}</p>
					            <p class="pro_list_top_proNum">项目编号：${key.pro_sn}</p>
					        </div>
					        <div class="pro_list_middle">
					        `
					        if(key.pro_method=='自主研发'){
					        	html += `
					        		<div class="pro_list_middle_bigO zz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='委托研发'){
								html += `
					        		<div class="pro_list_middle_bigO wt">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='集中研发'){
								html += `
					        		<div class="pro_list_middle_bigO jz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='合作研发'){
								html += `
					        		<div class="pro_list_middle_bigO hz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }
				            html += `
					            <ul class="pro_list_middle_info">
					                <li>开始时间：${key.pro_start||'-'}</li>
					                <li>结束时间：${key.pro_end||'-'}</li>
					                <li>经费预算：${key.pro_total||'-'}</li>
					            </ul>
					        </div>
					        <ul class="pro_list_bottom">
					            <li><a href="/admin/project/editPro?id=${key.id}">查看</a></li>
					            <li><a href="#" class='exportFile' data-id='${key.id}'>导出项目备查文件</a></li>
					        </ul>
					        <a href="" class="delete_btn" data-id='${key.id}'>删除</a>
					    </li>
					`
				}
				$('.pro_list').html(html)
			}
		})
	})



	// 导出文件
	$('.pro_list').on('click','.exportFile',function(e){
        e.preventDefault();
        let pro_id = $(this).attr('data-id')
        $.ajax({
            url:'/Admin/Project/getProYear',
            type:'get',
            data:{
                id:pro_id
            },
            success:function(data){
                if(data.code==1){
                    let datas = data.data
                    let html = ''
                    datas.forEach(key => {
                        html+=`<li>${key}</li>`
                    })
                    $('.choose_list').html(html)
                    window.sessionStorage['pro_id'] = pro_id
                    $('#choose').addClass('active')
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
            }
        })
    })


    $('.choose_list').on('click','li',function(e){
        window.sessionStorage['choose_year'] = $(this).html()
        $(this).addClass('active').siblings().removeClass('active')
    })

    $('.choose_btn').click(function(e){
        //防止重复点击按钮
        if( $(this).attr('lock') ) return
        let choose_year  = window.sessionStorage['choose_year']
        if(!choose_year || choose_year==''){
            $('#yz_modal').html('时间一定要选择！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }
        
        $(this).attr('lock','lock')

        // if($('#choose').find('.choose_btn').attr('export')=='huizong'){
        //     window.open(`/Admin/Project/exportTotal?year=${choose_year}`,"_blank");

        //     $('#choose').removeClass('active');
        //     $(this).removeAttr('lock')
        //     window.sessionStorage['choose_year'] = ''
        // }else if($('#choose').find('.choose_btn').attr('export')=='guiji'){
        //     // window.open(`/Admin/Project/exportTotal?year=${choose_year}`,"_blank");

        //     $('#choose').removeClass('active');
        //     $(this).removeAttr('lock')
        //     window.sessionStorage['choose_year'] = ''
        // }else{
            let pro_id  = window.sessionStorage['pro_id']
            
            $(this).removeAttr('lock')
            window.sessionStorage['pro_id'] = ''
            window.sessionStorage['choose_year'] = ''
            window.open(`/Admin/Project/exportPro?id=${pro_id}&year=${choose_year}`,"_blank");
        // }

    })

     //搜索
     $('#query_btn').click(function(){
     	let time = $('#choseYear_projectManage').find('option:selected').text()
     	let pro_info = $('#query_input').val()
     	$.ajax({
     		url:'/Admin/Project/getPros',
     		type:'get',
     		data:{
     			time:time,
     			pro_info:pro_info
     		},
     		success:function(data){
				let datas = data.data
				let html = `<li><a href="/Admin/Project/addPro" class="add_pro"></a></li>`
				for(let key of datas){
					html += `
						<li>
					        <div class="pro_list_top">
					            <p class="pro_list_top_proName">${key.pro_name}</p>
					            <p class="pro_list_top_proNum">项目编号：${key.pro_sn}</p>
					        </div>
					        <div class="pro_list_middle">
					        `
					        if(key.pro_method=='自主研发'){
					        	html += `
					        		<div class="pro_list_middle_bigO zz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='委托研发'){
								html += `
					        		<div class="pro_list_middle_bigO wt">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='集中研发'){
								html += `
					        		<div class="pro_list_middle_bigO jz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }else if(key.pro_method=='合作研发'){
								html += `
					        		<div class="pro_list_middle_bigO hz">
						                <div class="pro_list_middle_smallO"><span class="pro_list_middle_O_text">${key.pro_method}</span></div>
						            </div>
					        	`
					        }
				            html += `
					            <ul class="pro_list_middle_info">
					                <li>开始时间：${key.pro_start||'-'}</li>
					                <li>结束时间：${key.pro_end||'-'}</li>
					                <li>经费预算：${key.pro_total||'-'}</li>
					            </ul>
					        </div>
					        <ul class="pro_list_bottom">
					            <li><a href="/admin/project/editPro?id=${key.id}">查看</a></li>
					            <li><a href="#" class='exportFile' data-id='${key.id}'>导出项目备查文件</a></li>
					        </ul>
					        <a href="" class="delete_btn" data-id='${key.id}'>删除</a>
					    </li>
					`
				}
				$('.pro_list').html(html)
     		}
     	})
     })


    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.attr('id') == 'choose' && $this.parent().attr('id') != 'choose'){
            $('#choose').removeClass('active');
            window.sessionStorage['pro_id'] = ''
            window.sessionStorage['choose_year'] = ''
        }
    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }


    $('.pro_list').on('click','.delete_btn',function(e){
    	e.preventDefault()
    	// let del_id = $(this).attr('data-id')
    	window.sessionStorage['del_id'] = $(this).attr('data-id')
    	$('#delete').addClass('active')
    })

	$('#delete').on('click','button',function(){
		if($(this).html()=='确认'){
			let del_id = window.sessionStorage['del_id']
			$.ajax({
	            url:'/Admin/Project/deleteProj',
	            type:'post',
	            data:{
	                pro_id: del_id
	            },
	            success:function(data){
	                "use strict";
	                if(data.code==1){
	                    $('.pro_list').children('li').children(`a[data-id=${del_id}]`).parent().remove();
	                    $('#delete').removeClass('active')
	                }
	            }
	        })
		}else{
			$('#delete').removeClass('active')
		}
		window.sessionStorage['del_id'] = ''
	})

	
})